var util = require('../../utils/util.js');
var api = require('../../config/api.js');

Page({
    data: {
        // 商品分类
        navListcl:[],
        // 父分类id
        parentId:0,
        parentIddic:0,
        //判断选择框是否相同
        isboolon:'',
        //子分类列表
        dicchild:[],
        //子分类id
        category:0,
        // 商品列表
        goodlist:[],
        // ------------------------------------------------------------
        navList: [],
        categoryList: [],
        currentCategory: {},
        goodsCount: 0,
        nowIndex: 0,
        nowId: 0,
        list: [],
        allPage: 1,
        allCount: 0,
        size: 8,
        hasInfo: 0,
        showNoMore: 0,
        loading:0,
        index_banner_img:0,
        twodic:false
    },
  
      //查询商品分类
      getCommonDicType: function(){
        let that = this;
        util.request(api.CommonDicType).then(function (res) {
          // console.log("查分类--------")
          //   console.log(res)
            that.setData({
              "navListcl": res.data
          });
        })
      },

    onLoad: function(options) {
    },
    getChannelShowInfo: function (e) {
        let that = this;
        util.request(api.ShowSettings).then(function (res) {
          // console.log(this.data)
            if (res.errno === 0) {
                let index_banner_img = res.data.index_banner_img;
                that.setData({
                    index_banner_img: index_banner_img
                });
            }
        });
    },
    onPullDownRefresh: function() {
        wx.showNavigationBarLoading()
        this.getCatalog();
        wx.hideNavigationBarLoading() //完成停止加载
        wx.stopPullDownRefresh() //停止下拉刷新
    },
    getCatalog: function() {
        //CatalogList
        let that = this;
        util.request(api.CatalogList).then(function(res) {
            that.setData({
                navList: res.data.categoryList,
            });
        });
        util.request(api.GoodsCount).then(function(res) {
            that.setData({
                goodsCount: res.data.goodsCount
            });
        });
    },
    getCurrentCategory: function(id) {
        let that = this;
        util.request(api.CatalogCurrent, {
            id: id
        }).then(function(res) {
            that.setData({
                currentCategory: res.data
            });
        });
    },
    getCurrentList: function(id) {
      let that = this;
      if(id==0){
        util.request(api.GetGoodsList, {
        }).then(function(res) {
          // console.log("查全部-------------")
          // console.log(res)
          that.setData({
            "goodlist": res.data,
            "loading":0
          });
          // console.log(that.data.goodlist)

        });
        
      }else{
        util.request(api.GetGoodsList, {
          parentId: id,
          // category: idchild
        }).then(function(res) {
          that.setData({
            "goodlist": res.data,
            "loading":0
          });
          // console.log("查分类的list---------------")
          // console.log(res.data)
    });
      }



        // let that = this;
        // util.request(api.GetCurrentList, {
        //     size: that.data.size,
        //     page: that.data.allPage,
        //     id: id
        // }, 'POST').then(function(res) {
        //     if (res.errno === 0) {
        //         let count = res.data.count;
        //         that.setData({
        //             allCount: count,
        //             allPage: res.data.currentPage,
        //             list: that.data.list.concat(res.data.data),
        //             showNoMore: 1,
        //             loading: 0,
        //         });
        //         if (count == 0) {
        //             that.setData({
        //                 hasInfo: 0,
        //                 showNoMore: 0
        //             });
        //         }
        //     }
        // });
    },
    onShow: function() {

        //调用商品分类
        this.getCommonDicType();
        this.getCurrentList(0);



        this.getChannelShowInfo();
        let id = this.data.nowId;
        console.log("---------------------------")
        console.log(this.data)
        let nowId = wx.getStorageSync('categoryId');
        
        if(id == 0 && nowId === 0){
            return false
        }
        else if (nowId == 0 && nowId === '') {
            this.setData({
                list: [],
                allPage: 1,
                allCount: 0,
                size: 8,
                loading: 1
            })
            this.getCurrentList(0);
            this.setData({
                nowId: 0,
                currentCategory: {}
            })
            wx.setStorageSync('categoryId', 0)
        } else if(id != nowId) {
            this.setData({
                list: [],
                allPage: 1,
                allCount: 0,
                size: 8,
                loading: 1
            })
            this.getCurrentList(nowId);
            this.getCurrentCategory(nowId);
            this.setData({
                nowId: nowId
            })
            wx.setStorageSync('categoryId', nowId)
        }
        
        this.getCatalog();
    },
    switchCate: function(e) {
        // console.log(e.currentTarget.dataset)
        // console.log("-------------------------")
        let that = this
        let id = e.currentTarget.dataset.id;
        let nowId = this.data.nowId;
        that.data.parentIddic=id
        console.log(id)
        if(id!=0){
          util.request(api.GetTwoDic+id).then(function(res){
            // console.log("商城二级分类------------------")
            that.setData({
              "dicchild": res.data,
            });
            // console.log(that.data.dicchild)
        })
        that.data.isboolon=id;
        if(that.data.isboolon!=id){
          that.setData({
            "twodic": !that.data.twodic
          })
        }else{
          that.setData({
            "twodic": true
          })
        }
        }
        else{
          that.setData({
            "dicchild": null
          })
        }
        
        if (id == nowId) {
            return false;
        } else {
            this.setData({
                list: [],
                allPage: 1,
                allCount: 0,
                size: 8,
                loading: 1
            })
            if (id == 0) {
                this.getCurrentList(0);
                this.setData({
                    currentCategory: {}
                })
            } else {
                wx.setStorageSync('categoryId', id)
                this.getCurrentList(id);
                this.getCurrentCategory(id);
            }
            wx.setStorageSync('categoryId', id)
            this.setData({
                nowId: id
            })
        }
    },
    mySelect1: function(e){
      let that=this;
      let idchild = e.currentTarget.dataset.id;
      console.log(e)
      console.log(idchild)
      console.log(that.data.parentIddic)
      util.request(api.GetGoodsList,{
        parentId:that.data.parentIddic,
        category:idchild
      }).then(function(res){
        that.setData({
          "goodlist": res.data,
          "loading":0
        });
      }).catch((error) => {
        //分类异常
        console.log("分类异常")
        throw new Error(error);
      });
    },
    onBottom: function() {
        let that = this;
        if (that.data.allCount / that.data.size < that.data.allPage) {
            that.setData({
                showNoMore: 0
            });
            return false;
        }
        that.setData({
            allPage: that.data.allPage + 1
        });
        let nowId = that.data.nowId;
        if (nowId == 0 || nowId == undefined) {
            that.getCurrentList(0);
        } else {
            that.getCurrentList(nowId);
        }
    }
})